package com.qcxk.yuqian.rtu.controller;

import com.qcxk.yuqian.rtu.entity.Instruction;
import com.qcxk.yuqian.rtu.service.IInstructionService;
import com.qcxk.yuqian.system.utils.RedisUtil;
import com.qcxk.yuqian.system.utils.ThmsUtil;
import org.apache.commons.lang.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import com.qcxk.yuqian.system.component.behaviour.BehaviourOperator;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RestController;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.qcxk.yuqian.system.component.constant.SystemConstant;

import javax.servlet.http.HttpServletRequest;
import java.lang.reflect.Field;
import java.util.List;


/**
 * @auther lp
 * @create 2021-12-07 17:37:17
 * @describe 前端控制器
 */
@RestController
@RequestMapping("/instruction")
public class InstructionController {
    @Autowired
    private HttpServletRequest request;
    @Autowired
    private RedisUtil redisUtil;
    @Autowired
    private IInstructionService instructionService;

    @BehaviourOperator(desc = "查询Instruction信息")
    @RequestMapping(value = "/loadInstruction")
    public JSONObject loadInstruction(Instruction instruction) {
        JSONObject json = new JSONObject();
        instruction = instructionService.getById(instruction.getId());
        if (instruction != null) {
            json.put("code", "0");
            json.put("data", instruction);
        } else {
            json.put("code", "1");
            json.put("msg", SystemConstant.SYS_DATABASE_ERROR_CN);
        }
        return json;
    }

    @BehaviourOperator(desc = "查询Instruction列表")
    @RequestMapping(value = "/queryInstructionList")
    public JSONObject queryInstructionList(Page page, Instruction instruction) {
        JSONObject json = new JSONObject();
        page = instructionService.queryInstructionPage(page, instruction);
        json.put("code", "0");
        json.put("current", page.getCurrent());
        json.put("size", page.getSize());
        json.put("total", page.getTotal());

        List<Instruction> list = page.getRecords();

        json.put("dataList", list);
        return json;
    }

    @BehaviourOperator(desc = "单一属性修改Instruction")
    @RequestMapping(value = "/updateInstructionSingle")
    public JSONObject updateInstructionSingle(Integer id, String column, String value) throws NoSuchFieldException, IllegalAccessException {
        JSONObject json = new JSONObject();
        Instruction instruction = instructionService.getById(id);
        Class<? extends Instruction> clazz = instruction.getClass();// 获取到对象对应的class对象
        Field nameField = clazz.getDeclaredField(column);// 获取私有成员变量:column
        nameField.setAccessible(true);// 设置操作权限为true
        nameField.set(instruction, value);
        if (instructionService.updateById(instruction)) {
            json.put("code", "0");
        } else {
            json.put("code", "1");
            json.put("msg", SystemConstant.SYS_DATABASE_ERROR_CN);
        }
        return json;
    }

    @BehaviourOperator(desc = "整体修改Instruction")
    @RequestMapping(value = "/updateInstructionMultiple")
    public JSONObject updateInstructionMultiple(Instruction instruction) {
        JSONObject json = new JSONObject();

        if (instructionService.updateById(instruction)) {
            json.put("code", "0");
        } else {
            json.put("code", "1");
            json.put("msg", SystemConstant.SYS_DATABASE_ERROR_CN);
        }
        return json;
    }

    @BehaviourOperator(desc = "删除Instruction")
    @RequestMapping(value = "/deleteInstructionById")
    public JSONObject deleteInstructionById(Instruction instruction) {
        JSONObject json = new JSONObject();
        if (instructionService.deleteInstructionById(instruction)) {
            json.put("code", "0");
        } else {
            json.put("code", "1");
            json.put("msg", SystemConstant.SYS_DATABASE_ERROR_CN);
        }
        return json;
    }

    @BehaviourOperator(desc = "新增Instruction")
    @RequestMapping(value = "/addInstruction")
    public JSONObject addInstruction(Instruction instruction) {
        JSONObject json = new JSONObject();
        if (instructionService.save(instruction)) {
            json.put("code", "0");
        } else {
            json.put("code", "1");
            json.put("msg", SystemConstant.SYS_DATABASE_ERROR_CN);
        }
        return json;
    }

    @BehaviourOperator(desc = "新增指令")
    @RequestMapping(value = "/setIPAndPort")
    public JSONObject setIPAndPort(String ip, String rtuNo, String port) {
        Integer id = Integer.parseInt((String) redisUtil.get(request.getHeader("token")));
        JSONObject json = new JSONObject();
        if (StringUtils.isNotBlank(rtuNo)) {
            if (StringUtils.isNotBlank(ip)) {
                Instruction instruction = new Instruction();
                instruction.setCon(ip);
                instruction.setCtime(ThmsUtil.getDate());
                instruction.setRtuNo(rtuNo);
                instruction.setType("0");
                instruction.setUserId(id);
                instructionService.save(instruction);
                json.put("code", "0");
            }
            if (StringUtils.isNotBlank(port)) {
                Instruction instruction = new Instruction();
                instruction.setCon(port);
                instruction.setCtime(ThmsUtil.getDate());
                instruction.setRtuNo(rtuNo);
                instruction.setType("1");
                instruction.setUserId(id);
                instructionService.save(instruction);
            }
        } else {
            json.put("code", "1");
            json.put("msg", "设备编号不能为空");
        }
        return json;
    }
}

